package com.example.li_han.service;

import com.example.li_han.dao.UserDao;
import com.example.li_han.pojo.User;
import java.util.List;

public class UserService {
    UserDao userDao = new UserDao();

    /**
     * 用户登录验证 - 从数据库验证
     */
    public User login_check(String zhangHao, String password){
        return userDao.getUserByLogin(zhangHao, password);
    }

    /**
     * 用户注册（添加用户）
     */
    public int addUser(User user){
        return userDao.addUser(user);
    }

    /**
     * 删除用户
     */
    public int deleteUser(int userId){
        return userDao.deleteUser(userId);
    }


    /**
     * 根据ID查询用户
     */
    public User getUserById(int userId){
        return userDao.getUserById(userId);
    }

    /**
     * 验证用户密码
     */
    public boolean validatePassword(int userId, String password) {
        User user = userDao.getUserById(userId);
        return user != null && user.getPassword().equals(password);
    }

    /**
     * 更新用户密码
     */
    public int updatePassword(int userId, String newPassword) {
        User user = userDao.getUserById(userId);
        if (user != null) {
            user.setPassword(newPassword);
            return userDao.updateUser(user);
        }
        return 0;
    }

    /**
     * 查询所有用户
     */
    public List<User> getAllUsers(){
        return userDao.getAllUsers();
    }

    /**
     * 根据账号查询用户
     */
    public User getUserByZhangHao(String zhangHao){
        return userDao.getUserByZhangHao(zhangHao);
    }

    /**
     * 检查账号是否已存在
     */
    public boolean isZhangHaoExist(String zhangHao){
        User user = userDao.getUserByZhangHao(zhangHao);
        return user != null;
    }

    /**
     * 验证用户登录
     */
    public boolean validateLogin(String zhangHao, String password){
        User user = userDao.getUserByLogin(zhangHao, password);
        return user != null;
    }
}